<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui" template="/mainTemplate.xhtml">

    <ui:define name="content">
        <h:form id="materialForm" >

            <p:toolbar id="materialsToolbar" style="margin-top:20px; margin-bottom:5px">
                <p:toolbarGroup align="left">
                    <p:commandButton value="Добавить"
                                     actionListener="#{materialsBean.addMaterial()}" style="font-size: 85% !important"/>
                    <p:commandButton value="Редактировать"
                                     actionListener="#{materialsBean.edit()}" style="font-size: 85% !important"/>
                    <p:commandButton value="Приход материала"
                                     actionListener="#{materialsBean.coming()}" style="font-size: 85% !important"/>
                    <p:commandButton value="Расход материала"
                                     actionListener="#{materialsBean.expense()}" style="font-size: 85% !important"/>

                </p:toolbarGroup>
            </p:toolbar>

            <p:dataTable id="materialTable" var="material" value="#{materialsBean.materialsList}"
                         rowKey="#{material.name}" selection="#{materialsBean.selectedEquipment}"
                         selectionMode="single">

                <p:column headerText="Название">
                    <h:inputHidden value="#{material.id}"/>#{material.name}
                </p:column>

                <p:column headerText="Тип материала">
                    #{materialsBean.getMaterialType(material.id)}
                </p:column>

                <p:column headerText="Единицы измерения">
                    #{materialsBean.getMaterialUnits(material.id)}"
                </p:column>

                <p:column headerText="Описание">
                    #{material.description}"
                </p:column>

            </p:dataTable>

        </h:form>

        <h:form id="addMaterialDialogForm">
            <p:dialog id="addMaterialDialog" widgetVar="addMaterialDialog" header="Добавление материала" modal="true">
                <ui:include src="dialogs/addMaterial.xhtml"/>
            </p:dialog>
            <script type="text/javascript">
                function handleAddMaterialRequest(xhr, status, args) {
                    if(args.validationFailed || !args.saveMaterial) {
                        jQuery('#addMaterialDialog').effect("shake", { times:3 }, 100);
                    } else {
                        addMaterialDialog.hide();
                    }
                }
            </script>
        </h:form>
        <h:form id="editMaterialDialogForm">
            <p:dialog id="editMaterialDialog" widgetVar="editMaterialDialog" header="Добавление материала" modal="true">
                <ui:include src="dialogs/editMaterial.xhtml"/>
            </p:dialog>
            <script type="text/javascript">
                function handleEditMaterialRequest(xhr, status, args) {
                    if(args.validationFailed || !args.saveEdit) {
                        jQuery('#editMaterialDialog').effect("shake", { times:3 }, 100);
                    } else {
                        editMaterialDialog.hide();
                    }
                }
            </script>
        </h:form>
        <h:form id="comingMaterialDialogForm">
            <p:dialog id="comingMaterialDialog" widgetVar="comingMaterialDialog" header="Добавление материала" modal="true">
                <ui:include src="dialogs/materialComing.xhtml"/>
            </p:dialog>
            <script type="text/javascript">
                function handleComingMaterialRequest(xhr, status, args) {
                    if(args.validationFailed || !args.saveComing) {
                        jQuery('#comingMaterialDialog').effect("shake", { times:3 }, 100);
                    } else {
                        comingMaterialDialog.hide();
                    }
                }
            </script>
        </h:form>
        <h:form id="expenseMaterialDialogForm">
            <p:dialog id="expenseMaterialDialog" widgetVar="expenseMaterialDialog" header="Добавление материала" modal="true">
                <ui:include src="dialogs/expenseMaterial.xhtml"/>
            </p:dialog>
            <script type="text/javascript">
                function handleExpenseMaterialRequest(xhr, status, args) {
                    if(args.validationFailed || !args.saveExpense) {
                        jQuery('#expenseMaterialDialog').effect("shake", { times:3 }, 100);
                    } else {
                        expenseMaterialDialog.hide();
                    }
                }
            </script>
        </h:form>
    </ui:define>
</ui:composition>